package com.cw.realtime.common
package bean

import util._

import io.circe.Decoder.Result
import io.circe.{Decoder, HCursor}

import java.sql.ResultSet

case class TableProcessDim(
  sourceTable: String, // 来源维度表
  sinkTable: String, // 目标表名
  sinkFamily: String, // 数据到 hbase 的列族
  sinkColumns: String, // 输出字段
  sinkRowKey: String, // sink到 hbase 的时候的主键字段
  op: String = "r" // 配置表操作类型
) {


  def changeOp: String => TableProcessDim =
    TableProcessDim(sourceTable, sinkTable, sinkFamily, sinkColumns, sinkRowKey, _)


}


object TableProcessDim {

  implicit val decoder = new Decoder[TableProcessDim] {
    override def apply(c: HCursor): Result[TableProcessDim] = {
      for {
        op <- c.downField("op").as[String]
        decoder =
          Decoder.forProduct5("source_table", "sink_table", "sink_family", "sink_columns", "sink_row_key") {
            TableProcessDim(_, _, _, _, _, op)
          }
        table <-
          if ("d" == op) c.downField("before").as[TableProcessDim](decoder)
          else c.downField("after").as[TableProcessDim](decoder)
      } yield table

    }
  }

  implicit val qu = new Queryable[TableProcessDim] {
    override def decode(result: ResultSet): TableProcessDim = {
      val params = (1 to 5).map(result.getString)
      TableProcessDim(params(0), params(1), params(2), params(3), params(4))
      //      createCaseClass[TableProcessDim](result)
    }
  }

}
